package com.ctshk.task.worker.processors;

import com.ctshk.common.utils.HktBusUtil;
import com.ctshk.rpc.system.service.ISysUserService;
import com.github.kfcfans.powerjob.worker.core.processor.ProcessResult;
import com.github.kfcfans.powerjob.worker.core.processor.TaskContext;
import com.github.kfcfans.powerjob.worker.core.processor.sdk.BasicProcessor;
import com.github.kfcfans.powerjob.worker.log.OmsLogger;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @description -中港通API产品维护  每日更新一遍
 * @author 彭柳霖
 * @date 2021/3/12
 */
@Slf4j
@Component
public class ZgtBusProcessor implements BasicProcessor {

    @DubboReference
    private ISysUserService sysUserService;

    @Override
    public ProcessResult process(TaskContext context) throws Exception {

        OmsLogger omsLogger = context.getOmsLogger();


        SimpleDateFormat sdf2 = new SimpleDateFormat("YYYY-MM-dd");
        String departureDate = sdf2.format(new Date());

        //查询所有路线
        Map<String,Object> result = HktBusUtil.getRoutes();
        Map routes = (Map)result.get("routes");
        List<Map> route =  (List<Map>)routes.get("route");

        List<Map> mapList = new ArrayList<>();
        //根据路线查询班次信息
        for(int i=0;i<route.size();i++){
            Map<String,String> paramMap = new HashMap<>();
            paramMap.put("route_id",(String)route.get(i).get("routeId"));
            paramMap.put("departure_date",departureDate);
            paramMap.put("currency_id","1");
            Map<String,Object> shiftResult = HktBusUtil.getBusLines(paramMap);
            Map runs = (Map)shiftResult.get("runs");
            List<Map> run =  (List<Map>)runs.get("run");
            mapList.addAll(run);
        }


        sysUserService.insertCacheUser();
        // 根据控制台参数判断是否成功
        boolean success = !"failed".equals(context.getJobParams());
        omsLogger.info("InquiryExpiresProcessor finished process,success: .", success);

        return new ProcessResult(success, context + ": " + success);
    }

}
